package cn.edu.ctbu.firstdata.restcontroller;

import cn.edu.ctbu.firstdata.domain.Student;
import cn.edu.ctbu.firstdata.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/webapi/student")
public class StudentRestController {

    @Autowired
    IStudentService  studentService;

    @GetMapping("list")
    public List<Student>  GetAll(){

        return studentService.getAll();


    }

    /**
     * 示例：http://localhost:8080/webapi/student/2
     * 其中2表示id
     * @param id
     * @return
     */
    @GetMapping(value = "{id}")
    public  Student GetById(@PathVariable Integer id){

        return studentService.findById(id);
    }


    @DeleteMapping(value = "delete/{id}")
    public boolean DeleteById(@PathVariable Integer id){

        studentService.delete(id);

        return true;

    }

    @PostMapping(value = "add")
    public Integer Add(@RequestBody  Student student){

        studentService.save(student);
        return student.getId();
    }

    @PutMapping(value = "update")
    public Integer Update(@RequestBody  Student student){

        studentService.save(student);
        return student.getId();
    }



}
